package com.helloworld.upload.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helloworld.upload.domain.ColumnInfo;
import com.helloworld.upload.domain.UploadInfo;

public class SqlUtil {
	private static final Logger logger = LoggerFactory.getLogger(SqlUtil.class);
	
	private static final String SQL_FMT_DEL = "DELETE FROM {TABLE} FROM {WHERE}";
	private static final String SQL_FMT_INS = "INSERT INTO {TABLE} ({COLUMNS}) VALUES ({VALUES})";
	
		/**
		 * TODO: 목표데이터타입에 따라 SQL 구문 생성
		 * @param info
		 * @param value
		 * @return
		 */
		private static String addWhereEq(ColumnInfo info, Object value) {
			switch (info.getFieldType()) {
			default:
			}
			return "";
		}
		
		private static ColumnInfo getColumnInfo(List<ColumnInfo> columnInfos, int columnIndex) {
			for (ColumnInfo columnInfo : columnInfos) {
				if (columnInfo.getSourceColumnIndex() == columnIndex) { return columnInfo; }
			}
			return null;
		}
		
	public static String makeDeleteSql(UploadInfo uploadInfo, Map<Integer, Object> values) {
		String result = "";
		
		StringBuilder where = new StringBuilder();
		logger.debug("keySet:" + values.keySet());
		logger.debug("uploadInfo.getColumns():" + uploadInfo.getColumns());
		for (int columnIndex : values.keySet()) {
			ColumnInfo info = getColumnInfo(uploadInfo.getColumns(), columnIndex);
			
			logger.debug("value:" + values.get(columnIndex) + ", target:" + info.getTargetColumn());
			if (!StringUtils.equals("", where.toString())) { where.append(" AND "); }
			where.append(info.getTargetColumn()).append("=").append(values.get(columnIndex));
		}
		
		result = StringUtils.replace(SQL_FMT_DEL, "{TABLE}", uploadInfo.getTargetTable());
		result = StringUtils.replace(result, "{WHERE}", where.toString());
				
		return result;
	}
	
}
